package com.rimi.bbs.bean;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.util.HashMap;

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class JsonResult extends HashMap<String, Object> {

    private int code;
    private String msg;
    private String url;
    private Object data;


    public static JsonResult okHome(String msg){
        return ok(msg).put("action","/index");
    }

    /**
     * @return
     *     返回成功
     */
    public static JsonResult ok(){
        return new JsonResult(0,"操作成功");
    }
    /**
     * @param msg
     * @return
     *     返回成功
     */
    public static JsonResult ok(String msg){
        return new JsonResult(0,msg);
    }

    /**
     * @param
     * @return
     *     返回成失败
     */
    public static JsonResult error(){
        return new JsonResult(500,"操作失败");
    }

    /**
     * @param msg
     * @return
     *     返回失败
     */
    public static JsonResult error(String msg){
        return new JsonResult(500,msg);
    }

    public JsonResult(int code, String msg){
        this.put("code",code);
        this.put("msg",msg);
    }

    /**
     * 放入object
     */
    @Override
    public JsonResult put(String key, Object object) {
        super.put(key, object);
        return this;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}
